package com.dmpb.ping.graph;

import com.dmpb.ping.core.PingCore;
import com.dmpb.ping.core.Sound;
import com.dmpb.ping.core.Utils;

import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ysb on 2017/11/12.
 */
public class Ping4 {

  private static Icon ICON_RED;
  private static Icon ICON_BLACK;
  private static Icon ICON_GREEN;
  private JPanel jPanel;
  private JButton button1;
  private JTextArea resultTextArea;
  private JLabel jLabel1;
  private JLabel jLabel2;
  private JLabel jLabel3;
  private JLabel jLabel4;
  private JButton 停止Button;
  public JLabel jLabelBlack;
  public JLabel jLabelRed;
  public JLabel jLabelGreen;
  private JButton 录入数据Button;
  private JTextField labelTextField4;
  private JTextField labelTextField3;
  private JTextField labelTextField2;
  private JTextField labelTextField1;
  private JLabel textLabel1;
  private JLabel textLabel2;
  private JLabel textLabel3;
  private JLabel textLabel4;
  private JLabel jLabelOrigin;
  private JButton 自动运行Button;
  private JTextField loopTimeText;
  private JLabel loopTimeLabel;
  public static List<String> ip = new ArrayList<>();
  public static List<JLabel> iconResult = new ArrayList<>();
  public static Integer loopTime = 3;
  public static boolean isRunning = false;
  public static PingThread pingThread;
  public static SoundThread soundThread;

  public Ping4() {

    button1.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        isRunning = false;
        clearConsole();
        appendConsole("手动测试连通性结果如下：\n");
        List<Boolean> ipPingResult = beginPing();
        updateIconResult(ipPingResult);
        updateConsoleResult(ipPingResult);
        alarm(ipPingResult);
      }
    });

    录入数据Button.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        isRunning = false;
        if(!loopTimeText.getText().equals("")){
          if (!Utils.isNumeric(loopTimeText.getText().trim()) ||
              Double.valueOf(loopTimeText.getText().trim()).intValue() < 3) {
            setConsole("设定的轮训时间必须是大于或等于3的整数");
            return;
          }
          loopTime = Double.valueOf(loopTimeText.getText().trim()).intValue();
          loopTimeLabel.setText(loopTime.toString());
        }
        jLabel1.setText(labelTextField1.getText());
        jLabel2.setText(labelTextField2.getText());
        jLabel3.setText(labelTextField3.getText());
        jLabel4.setText(labelTextField4.getText());

        ip.clear();
        ip.add(jLabel1.getText().trim());
        ip.add(jLabel2.getText().trim());
        ip.add(jLabel3.getText().trim());
        ip.add(jLabel4.getText().trim());
        setConsole("录入成功!!");
      }
    });
    自动运行Button.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        setConsole("程序开始运行...\n");
        if(loopTimeLabel.getText().equals("")){
          setConsole("请设定轮训时间!!");
          return;
        }
        pingThread = new PingThread();
        isRunning = true;
        Thread ping_thread = new Thread(pingThread);
        ping_thread.start();
      }
    });
    停止Button.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        isRunning = false;
        appendConsole("停止轮询!!");
      }
    });
  }

  public static void alarm(List<Boolean> ipPingResult) {
    for (int i = 0; i < 4; i++) {
      if (ipPingResult.get(i).equals(Boolean.FALSE)) {
        new Thread(new SoundThreadOneTime()).start();
        break;
      }
    }
  }

  private void alarmAuto(List<Boolean> ipPingResult) {
    for (int i = 0; i < 4; i++) {
      if (ipPingResult.get(i).equals(Boolean.FALSE)) {
        soundThread = new SoundThread();
        Thread thread = new Thread(soundThread);
        thread.start();
      }
    }
  }


  private void updateConsoleResult(List<Boolean> ipPingResult) {
    String resultString = analyzeString(ipPingResult);
    appendConsole(resultString);
  }

  private void updateIconResult(List<Boolean> ipPingResult) {
    for (int i = 0; i < 4; i++) {
      if (ipPingResult.get(i).equals(Boolean.FALSE)) {
        iconResult.get(i).setIcon(ICON_RED);
      } else {
        iconResult.get(i).setIcon(ICON_GREEN);
      }
    }
  }

  public static List<Boolean> beginPing() {
    List<Boolean> ipPingResult = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
      try {
        ipPingResult.add(PingCore.ping(ip.get(i)));
      } catch (Exception e1) {
        ipPingResult.add(false);
      }
    }
    return ipPingResult;
  }

  private String analyzeString(List<Boolean> ipPingResult) {
    String result = "以下ip出现问题";
    boolean isOk = true;
    for (int i = 0; i < ipPingResult.size(); i++) {
      if (ipPingResult.get(i).equals(Boolean.FALSE)) {
        isOk = false;
        result = result.concat("ip:").concat(ip.get(i)).concat("无连接\n");
      }
    }
    if (isOk) return "全部连通!!";
    return result;
  }

  private void appendConsole(String text) {
    String originText = resultTextArea.getText();
    resultTextArea.setText(originText + text);
  }

  private void setConsole(String text) {
    resultTextArea.setText(text);
  }

  private void clearConsole() {
    resultTextArea.setText("");
  }


  private void doCreate(Ping4 ping4) {
    JFrame frame = new JFrame("网络连通性监控系统");
    frame.setContentPane(ping4.jPanel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }

  public static void main(String[] args) {
    Ping4 ping4 = new Ping4();
    initPing8(ping4);
    ping4.doCreate(ping4);
  }

  private static void initPing8(Ping4 ping4) {
    ICON_BLACK = ping4.jLabelBlack.getIcon();
    ICON_RED = ping4.jLabelRed.getIcon();
    ICON_GREEN = ping4.jLabelGreen.getIcon();
    ping4.jLabelBlack.hide();
    ping4.jLabelRed.hide();
    ping4.jLabelGreen.hide();
    ping4.jLabelOrigin.setIcon(ICON_GREEN);
    ping4.labelTextField1.setText("127.0.0.1");
    ping4.labelTextField2.setText("127.0.0.1");
    ping4.labelTextField3.setText("127.0.0.1");
    ping4.labelTextField4.setText("127.0.0.1");
    ping4.loopTimeText.setText("3");
    iconResult.add(ping4.jLabel1);
    iconResult.add(ping4.jLabel2);
    iconResult.add(ping4.jLabel3);
    iconResult.add(ping4.jLabel4);
  }



  public class PingThread implements Runnable {

    public void run() {
      long startTime = System.currentTimeMillis();
      while(isRunning){
        long nowTime = System.currentTimeMillis();
        if(nowTime-startTime > loopTime * 1000){
          startTime = nowTime;
          List<Boolean> ipPingResult = beginPing();
          alarmAuto(ipPingResult);
          updateIconResult(ipPingResult);
          updateConsoleResult(ipPingResult);
        }
      }
    }
  }

  public static void alert100times(){
    Sound sound = new Sound();
    for(int i=0;i<100;i++){
      if(isRunning)sound.alarm();
    }
  }

  public static class SoundThread implements Runnable {
    public void run() {
      alert100times();
    }
  }

  public static class SoundThreadOneTime implements Runnable {
    public void run() {
      Sound sound = new Sound();
      sound.alarm();
    }
  }

}
